# from django.http import HttpResponse
from django.shortcuts import render,HttpResponse
from django.urls import reverse
import datetime
from book.models import Author
# Create your views here.
# 在url中携带参数
# 查询字符串参数
# from book.models import Article
from django.db import connection
from .forms import MyForm

# Create your views here.
def show_database_information(request):
    #获取游标对象
    cursor =connection.cursor()
    #拿到游标对象后执行sql语句
    cursor.execute("select * from demo")
    #获取所有的数
    rows = cursor.fetchall()
    # 遍历查询到的数据
    for row in rows:
        print(row)
    return HttpResponse("执行成功")

def trans2(request):
    return render(request,"trans.html")
def book_show(request):
    class Person:
        def __init__(self,name):
            self.name = name
    info = {"author":"施耐庵","name":"水浒传","Person":Person("施耐庵"),"range":[1,2,3],
            "info1":"张三","info2":datetime.datetime.now(),"info3":'hello world,hello world!',
            "info4":""}


    return render(request,"book_show_information.html",context=info)
def book_detail_query_string(request):
    book_id = request.GET.get('id')
    book_title = request.GET.get('title')
    return HttpResponse(f"您查找的图书id是{book_id},图书名称是{book_title}")

def book_detail_int(request,book_id):
    print(reverse("book_detail_query_string"))
    return HttpResponse(f"您查找的图书id是{book_id}")

def book_detail_str(request,book_title):
    return HttpResponse(f"您查找的图书是{book_title}")

def book_detail_path(request,book_path):
    return HttpResponse(f"您查找的图书是{book_path}")

def book_detail_uuid(request,book_uuid):
    return HttpResponse(f"您查找的图书是{book_uuid}")

def book_detail_slug(request,book_slug):
    return HttpResponse(f"您查找的图书是{book_slug}")


def book_Author(request):
    author_1 = Author(1,"吴承恩","18","男")
    author_1.save()
    return HttpResponse("记录成功！")


def book_Article(request):
    form = MyForm()
    return render(request,"book_Article.html",context={"form":form})

def book_csrf_test(request):
    form = MyForm()
    if request.method == "GET":
        return render(request,"book_csrf_test.html",context={"form":form})
    if request.method == "POST":
        form = MyForm(request.POST)
        if form.is_valid():
            info = form.cleaned_data
        else:
            info = form.errors
        return HttpResponse(info)
